package com.pubinfo.passbook.data.controller.open;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.entity.TNewSourceInvoice;
import com.pubinfo.passbook.common.mapper.TFileInfoMapper;
import com.pubinfo.passbook.common.mapper.TNewSourceInvoiceMapper;
import com.pubinfo.passbook.common.mapper.TSourceInvoiceMapper;
import com.pubinfo.passbook.common.model.dto.open.InvoiceParam;
import com.pubinfo.passbook.common.model.dto.open.InvoiceResult;
import com.pubinfo.passbook.common.model.dto.open.WiteoffData;
import com.pubinfo.passbook.common.model.http.request.GainWriteoffInstStatus.GainWriteoffInstStatusItems;
import com.pubinfo.passbook.common.model.http.response.GetAutoCreateWriteoff.WriteoffMessage;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusMessage;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.data.service.OpenWiteoffService;
import com.pubinfo.passbook.data.service.TFileInfoService;
import com.pubinfo.passbook.data.service.VoucherFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping(value = "open")
@Api(tags = "财辅报账")
@Slf4j
public class OpenWiteoffController {
  @Autowired
  OpenWiteoffService witeoffService;
  @Autowired
  TFileInfoService tFileInfoService;
  @Autowired
  TFileInfoMapper tFileInfoMapper;
  @Autowired
  VoucherFileService voucherFileService;
  @Autowired
  TNewSourceInvoiceMapper tNewSourceInvoiceMapper;
  
  @PostMapping("createWriteoff")
  @ApiOperation(value = "发起报账")
  public JsonResult<WriteoffMessage> createWriteoff(WiteoffData data) {
    return JsonResult.success(witeoffService.createWriteoff(data));
  }
  
  @PostMapping("getWriteoffInstStatus/{systemCode}")
  @ApiOperation(value = "获取报账单状态")
  public WriteoffInstStatusMessage getWriteoffInstStatus(@RequestBody GainWriteoffInstStatusItems account, @PathVariable("systemCode") String systemCode) {
    return witeoffService.getWriteoffInstStatus(account, systemCode);
  }


  @PostMapping("/downloadOfd/{fpdm}/{fphm}")
  @GetMapping("/downloadOfd/{fpdm}/{fphm}")
  @ApiOperation(value = "根据发票代码和发票号码下载版式文件")
  public void downloadOfd(HttpServletRequest request, HttpServletResponse response, @PathVariable("fpdm")  String fpdm,@PathVariable("fphm")  String fphm) throws UnirestException, UnsupportedEncodingException {
    String fileId=tFileInfoMapper.selectFileIdByFpdmAndFphm(fpdm,fphm);
    if(fileId==null) {
      fileId=tFileInfoMapper.selectPassbookFileIdByInvoice(fpdm,fphm);
    }
    if(fileId!=null) {
      tFileInfoService.acctDownload(request, response,fileId);
    }
  }


  @PostMapping("/downloadOfdXmlByfPKEY/{fPKEY}")
  @GetMapping("/downloadOfdXmlByfPKEY/{fPKEY}")
  @ApiOperation(value = "根据发票校验码下载结构化数据文件名")
  public void downloadOfdXmlByfPKEY(HttpServletRequest request, HttpServletResponse response, @PathVariable("fPKEY")  String fPKEY) throws Exception {
    if(fPKEY.indexOf("_")!=-1)
    {
      String fileId=tFileInfoMapper.selectFileIdByFpdmAndFphm(fPKEY.split("_")[0],fPKEY.split("_")[1]);
      if(fileId==null) {
        fileId=tFileInfoMapper.selectPassbookFileIdByInvoice(fPKEY.split("_")[0],fPKEY.split("_")[1]);
      }
      if(fileId!=null) {
        voucherFileService.getOfdXml(request, response,tFileInfoMapper.selectById(fileId));
      }
    }else
    {
      TFileInfo fileInfo = tFileInfoMapper.selectFileIdByInvoiceId(fPKEY);
      if(fileInfo!=null) {
        voucherFileService.getOfdXml(request, response,fileInfo);
      }
    }
  }


  @PostMapping("/getInvoiceListByDate")
  @ApiOperation(value = "根据日期获取当天所有的发票代码和发票号码（日期格式为YYYY-MM-dd）")
  public List<InvoiceResult> getInvoiceListByDate(String date)
  {
    return tNewSourceInvoiceMapper.getInvoiceListByDate(date);
  }

  @PostMapping("/getInvoiceFile")
  @ApiOperation(value = "获取票据相关的文件")
  public void getInvoiceFile(HttpServletRequest request, HttpServletResponse response,InvoiceParam invoiceParam) throws Exception {
    voucherFileService.getInvoiceFile(request,response,invoiceParam);
  }
}
